<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin
 *
 * @author zwang
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class admin extends User_Controller {

    //put your code here
    function __construct() {
        parent::__construct();
        $username = $this->session->userdata('username');
        $current_userType = $this->get_userType($username);
        if ($current_userType != $this->type_admin) {
            $this->redirect($current_userType);
        }
        $this->load->model("database");
    }

    function index() {
        if ($this->session->userdata('validated')) {
            $data['username'] = $this->session->userdata('username');
//            echo $data['username'];
            $this->load->view('admin/adminPage', $data);
        } else {
            redirect('login', 'refresh');
        }
    }

    function logout() {
        $this->session->unset_userdata('validated');
        $this->session->unset_userdata('is_logged_in');
        session_unset();
        session_destroy();
        redirect('citportal');
    }

    function showNews() {
        $query = $this->database->getNews();
        $data = array();
        $i = 0;
        foreach ($query as $row) {
            $data[$i]['id'] = $row->id;
            $data[$i]['news'] = $row->news;
            $data[$i]['date'] = $row->date;
            $i++;
        }
        $this->load->view('/admin/news', array('data' => $data));
    }

    function showPaper() {
        $papers = $this->database->getPapers();
        $data = $papers->result_array();
        $this->load->view("/admin/paper", array("data" => $data));
    }

    function showAbout() {
        $this->load->view("/admin/about");
    }

    function showTutorials() {
        $this->load->view("tutorials");
    }

    function showPeople() {
        $this->load->view("/admin/people");
    }

    function showLinks() {
        $links = $this->database->getLinks();
        $data = array();
        $data = $links->result_array();
        $headers = array();
        $previous = '';
        $i = 0;
        foreach ($links->result() as $link) {
            if ($previous != $link->header) {
                $headers[$i] = $link->header;
                $previous = $link->header;
                $i++;
            }
            continue;
        }
        $this->load->view("admin/links", array('headers' => $headers, 'data' => $data));
    }

    function submitLink() {
        $name = $this->input->get_post('name');
        $link = $this->input->get_post('link');
        $header = $this->input->get_post('header');
        $type = $this->input->get_post('category');
        $description = $this->input->get_post('description');
        if ($this->database->insertLink($name, $link, $header, $type, $description)) {
            echo "success";
            return;
        }
        return false;
    }

    function deleteLink() {
        $id = $this->input->get_post('id');
        if ($this->database->deleteLink($id)) {
            return true;
        }
        return false;
    }

    function updateLink() {
        $id = $this->input->get_post('id');
        $name = $this->input->get_post('name');
        $header = $this->input->get_post('header');
        $link = $this->input->get_post('link');
        $type = $this->input->get_post('type');
        $description = $this->input->get_post('description');

        if ($this->database->updateLink($id, $name, $header, $link, $type, $description)) {
            echo "success";
            return TRUE;
        }
        echo "fail";
        return false;
    }

    function showTools() {
        
    }

    function showFeedbacks() {
        $query = $this->database->getFeedback();
        $data = array();
        $i = 0;
        foreach ($query as $row) {
            $data[$i]['id'] = $row->id;
            $data[$i]['date'] = $row->timestamp;
            $data[$i]['name'] = $row->name;
            $data[$i]['affiliation'] = $row->affiliation;
            $data[$i]['email'] = $row->email;
            $data[$i]['category'] = $row->category;
            $data[$i]['comment'] = $row->comment;
            $i++;
        }
        $this->load->view('/admin/feedback', array('data' => $data));
    }

    function deleteFeedback() {
        $id = $this->input->get_post('id');
        if ($this->database->deleteFeedback($id)) {
            return true;
        }
        return false;
    }

    function addNews() {
        $date = $this->input->get_post('date');
        $news = $this->input->get_post('news');
        $query = $this->database->insertNews($date, $news);
        if ($query) {
            echo "true";
        }
        return FALSE;
    }

    function updateNews() {
        $id = $this->input->get_post('id');
        $news = $this->input->get_post('news');
        $date = $this->input->get_post('date');
        if ($this->database->updateNews($id, $news, $date)) {
            echo "success";
            return TRUE;
        }
        echo "fail";
        return false;
    }

    function deleteNews() {
        $id = $this->input->get_post('id');
        if ($this->database->deleteNews($id)) {
            echo "success";
            return true;
        }
        echo "fail";
        return false;
    }

    function uploadPaper() {
        $description = $this->input->get_post('description');
        $date = $this->input->get_post('date');
        $name = $this->input->get_post('name');
        $file = $this->input->get_post('file');

        $filepath = 'uploads/papers/';
        if (!is_dir($filepath)) {
            mkdir($filepath, 0755, TRUE);
        }
        $data = array();
        $data = $this->upload_file('file', $filepath);
        if ($data['status'] == "error") {
            echo json_encode(array('status' => 'fail', 'msg' => $data['msg']));
            return;
        }
        $filename = $data['file_name'];

        if ($this->database->insertPaper($filename, $filepath, $date, $name, $description)) {
            echo json_encode(array('status' => 'success', 'msg' => 'Paper has been successfully uploaded!'));
        } else {
            echo json_encode(array('status' => 'fail', 'msg' => 'Paper failed to upload!'));
        }
    }

    function loadUpdatePaper_view() {
        $id = $this->input->get_post('id');
        $data = $this->database->getPaperByID($id);
        $this->load->view("admin/updatePaper", array('data' => $data));
    }

    function updatePaper() {
        $id = $this->input->get_post('id');
        $paper = $this->database->getPaperByID($id);
        $description = $this->input->get_post('description');
        $date = $this->input->get_post('date');
        $name = $this->input->get_post('name');
        $file = $this->input->get_post('file');
        $filepath = $paper['filepath'];
        if (!is_dir($filepath)) {
            mkdir($filepath, 0755, TRUE);
        }
//        $data = array();
        $data = $this->upload_file('update_file', $filepath);
        if ($file != '') {
            if ($data['status'] == "error") {
                echo json_encode(array('status' => 'fail', 'msg' => $data['msg']));
                return;
            }
            $filename = $data['file_name'];
        } else {
            $filename = $paper['filename'];
        }

        if ($this->database->updatePaper($id, $filename, $filepath, $date, $name, $description)) {
            echo json_encode(array('status' => 'success', 'msg' => 'Paper has been successfully updated!'));
        } else {
            echo json_encode(array('status' => 'fail', 'msg' => 'No change made!'));
        }
    }

    function deletePaper() {
        $id = $this->input->get_post('id');
        if ($this->database->deletePaper($id)) {
            echo "success";
            return true;
        }
        return false;
    }

}

?>
